$(function() {
    $(document).on('click', function() {
        $('.login_btn_list').height('0');
    })
    
	//  清除所有session
    sessionStorage.clear();
    
    // 点击登录
    $('.def_login').on('click', function(e) {
        $height = $('.login_btn_list');
        if ($height.height() == 0) {
            $height.height('100px')
        } else {
            $height.height('0')
        }
        // 防止冒泡
        e.stopPropagation();
    })
    // // 防止冒泡
//    $('.login_btn_list').on('click', function(e) {
//        e.stopPropagation();
//    })
    
    
    var you=GetQueryString("rid");
	function GetQueryString(name){
	     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
	     var r = window.location.search.substr(1).match(reg);
	     if(r!=null)return  unescape(r[2]); return null;
	}

    // 选择按钮弹框
    $('#exampleModal').on('show.bs.modal', function(event) {
        var button = $(event.relatedTarget);
        var recipient = button.data('whatever');
        var modal = $(this);
        modal.find('.modal-body input').attr("placeholder", recipient);
        if (recipient == "请输入ETH钱包地址") {
            modal.find('.modal-body input').attr("data-type", 1);
        } else {
            modal.find('.modal-body input').attr("data-type", 0);
        }
    })
    var prevent_repetition = true;
    $(".bound_btn").on("click", function() {
        if (prevent_repetition) {
            prevent_repetition = false;
            var walletType = $("#recipient-name").attr("data-type");
            var address = $("#recipient-name").val();
            if (!address) {
                alert("请输入钱包地址");
                prevent_repetition = true;
                return false;
            }
            // 判断BTC和ETH
            if (walletType == 1) {
                //检验一个地址的格式是否正确
                var isTrue = web3.isAddress(address);
                if (!isTrue) {
                    alert("钱包地址不正确，请重新输入");
                    prevent_repetition = true;
                    return false;
                }
                kkk(address,walletType);
            } else {
                if (address.length < 26 || address.length > 35) {
                    alert("钱包地址不正确，请重新输入");
                    prevent_repetition = true;
                    return false;
                } else {
                    $.ajax({
                        type: "get",
                        url: 'https://chain.api.btc.com/v3/address/' + address,
                        success: function(result) {
                            if (result.err_no != 0) {
                                alert("钱包地址不正确，请重新输入");
                                prevent_repetition = true;
                                return false;
                            }
                            kkk(address,walletType);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert("请求失败："+XMLHttpRequest.status);    
                        }
                    });
                }
            }
            
        }
    })
	
	function kkk(address,walletType){
		var json = {
            "address": address,
            "walletType": walletType
        }
        $.ajax({
            type: "post",
            url: url + '/bigWinner/api/login/entry',
            data: JSON.stringify(json),
            dataType: 'json',
            contentType: "application/json",
            success: function(result) {
                if (result.success) {
                    alert("登录成功！");
                    sessionStorage.setItem("playId", result.obj.account.playId);
                    sessionStorage.setItem("logonToken", result.obj.logonToken);
                    sessionStorage.setItem("walletType", result.obj.walletType);
                    
                    location.href = "index.html?BE_type=" + $('#recipient-name').attr("data-type");
                } else {
                    if (result.status == "0") {
                        alert(result.msg);
                        $("#exampleModal").modal("hide");
                        $("#exampleModal1").modal("toggle");
                    } else {
                        isErr(result);
                    }
                }
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("请求失败："+XMLHttpRequest.status); 
            },
            complete: function() {
                prevent_repetition = true;
            }
        });
	}

    var prevent_repetition_1 = true;
    $(".pay_btn").on("click", function() {
        if (prevent_repetition_1) {
            prevent_repetition_1 = false;
            var walletType = $("#recipient-name").attr("data-type");
            var address = $("#recipient-name").val();
            var payPassword = $("#recipient-name1").val();
            if (!payPassword) {
                alert("请输入支付密码");
                prevent_repetition_1 = true;
                return false;
            }
            if(you == null || you == undefined || you == ""){
            	var json = {
	                "address": address,
	                "walletType": walletType,
	                "payPassword": payPassword
	            }
            }else{
            	var json = {
	                "address": address,
	                "walletType": walletType,
	                "payPassword": payPassword,
	                "rid":you
	            }
            }
            
            $.ajax({
                type: "post",
                url: url + '/bigWinner/api/login/firstEntry',
                data: JSON.stringify(json),
                dataType: 'json',
                contentType: "application/json;charset=UTF-8",
                success: function(result) {
                    if (result.success) {
                        alert("登录成功！");
                        sessionStorage.setItem("playId", result.obj.account.playId);
                        sessionStorage.setItem("logonToken", result.obj.logonToken);
                        sessionStorage.setItem("walletType", result.obj.walletType);
                        
                        location.href = "index.html?BE_type=" + $('.modal-body input').attr("data-type");
                    } else {
                        isErr(result);
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("请求失败："+XMLHttpRequest.status); 
                },
                complete: function() {
                    prevent_repetition_1 = true;
                }
            });
        }
    })
})